var app = getApp();
//获得

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");

Page({
    data:{
        pageNo:0,
        pageCount:10
    },
    //请求数据
    Presenter: {
        init: function (that) {
        var user = Global.getUserStorage();
        var data = { "action": "list", "begin": that.data.pageNo,"end":that.data.pageCount,"order":"by addTime desc","accountId":user.id };
        MyRequest.MyGetWithToast("AccountLog", that, that.Binder.init, data);
        },
        LoadMore:function(that){
            var user = Global.getUserStorage();
        var data = { "action": "list", "begin": that.data.pageNo*that.data.pageCount,"end":that.data.pageCount,"order":"by addTime desc","accountId":user.id };
        MyRequest.MyGetWithToast("AccountLog", that, that.Binder.LoadMore, data);

        }
    },
    Binder:{
        init:function(result,that){
            var data = result.data.data;
            for(var i=0;i<data.length;i++){
                var m = data[i].afterMoney - data[i].beforeMoney;
                if(m == 0){
                    data.remove(i);
                }else if(m > 0){
                    data[i].moneyColor = "red";
                    m = "+" + data[i].money;
                    data[i].bMoney = m;
                }else{
                    data[i].moneyColor = "green";
                    m = "-" + data[i].money;
                    data[i].bMoney = m;
                }
            }
            that.setData({
                details:data
            });
        },
        LoadMore:function(result,that){
            var total = result.data.data.length;
            if(total > 0){
                var data = result.data.data;
                console.log(data)
                var newData = that.data.details;
                for(var i=0;i<data.length;i++){
                    var m = data[i].afterMoney - data[i].beforeMoney;
                    if(m == 0){
                        data.remove(i);
                    }else if(m > 0){
                        data[i].moneyColor = "red";
                        m = "+" + data[i].money;
                        data[i].bMoney = m;
                    }else{
                        data[i].moneyColor = "green";
                        m = "-" + data[i].money;
                        data[i].bMoney = m;
                    }
                    newData.push(data[i]);
                }
                that.setData({
                details:newData
                })
            }
        }
    }
    ,
    onLoad:function(){
        this.Presenter.init(this);
    },
    onReachBottom:function(){
        var pageNo =  this.data.pageNo + 1;
        this.setData({
            pageNo:pageNo
        });
        this.Presenter.LoadMore(this);
    },
  onPullDownRefresh:function(){
        this.setData({
            pageNo:0
        });
        this.Presenter.init(this);
  }
});